package futian
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.utils.setTimeout;
	import flash.utils.clearTimeout;
	import flash.text.TextField;
	import flash.filesystem.File;

	public class BgSelect extends Sprite
	{
		private var backBtn:BtnBase;
		private var uploadBtn:BtnBase;

		public function BgSelect()
		{
			_main = this;
			addChild(new Bitmap(test.main.bg.bitmapData));
			width = test.stageW;
			height = test.stageH;

			uploadBtn = new BtnBase(toUpload,test.newBtn("确认选择"));
			//uploadBtn = new BtnBase(toUpload,new Assets.upload_png);

			backBtn = new BtnBase(back,test.newBtn("返回"));//212x76
			//backBtn = new BtnBase(back,new Assets.back_png);

			backBtn.x = 1684;
			backBtn.y = 980; 
			uploadBtn.x = 796;
			uploadBtn.y = 876;
			// title: 692,84,528,104
			var title:TextField = ViewSet.makeTxtLine("请选择背景",80,0xffffff);
			addChild(title);
			title.y = 100;
			title.x = 1920/2-title.width/2;

			//list: 0,240,1920,588
			selector = new XiangkuangSelect();
			addChild(selector);

		}
		private var selector:XiangkuangSelect;

		public static function show():void
		{
			main.visible = true;
			test.main.addChild(main);
		}
		private static var _main:BgSelect= null;
		public static function get main():BgSelect
		{
			if(_main==null)
				_main = new BgSelect();
			return _main;
		}
		public static function back():void
		{
			main.visible = false;
			FirstPage.show();
		}
		private static var isSaving:Boolean = false;
		private function toUpload():void
		{
			if(isSaving)
				return;
			if(hasUploaded){
				Toast.show("照片已上传");
				return;
			}
			clearTimeout(timeoutId);
			Toast.show("上传中...",0);
			isSaving = true;
			//uploadBtn.visible = false;
			timeoutId = setTimeout(saving,400);
		}

		private static var hasUploaded:Boolean = false;
		private static var timeoutId:uint;
		private function saving():void
		{
			clearTimeout(timeoutId);
			//发送至副加屏
			var photofile:File = WriteData.saveBitmap("image/"+Datas.date2str()+"_"+(Datas.index+1)+".png",CameraPage.photoBmp);
			//var signfile:File = WriteData.saveDisplayObject("image/"+date2str()+"_s"+index+".png",SignPage.drawBoard);
			if(photofile)
			{
				if(FirstPage.photoPlayer)
					FirstPage.photoPlayer.addPhotoPath(photofile.url);
				Window2.photoPlayer.addPhotoPath(photofile.url);
				Window2.photoPlayer.show5s(photofile.url);


				Toast.show("上传成功");
				hasUploaded = true;
			}else{
				Toast.show("上传失败");
			}
			isSaving = false;
		}
	}
}
